<?php


namespace App\Http\Controllers;


use App\Models\User;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class TestController extends BaseApiControllers
{
    public function addrole(){
        $role1=Role::create(["name"=>"test1"]);
        $role2=Role::create(["name"=>"test2"]);
    }

    public function permission(){
        $permission1 = Permission::create(['name'=>'member']);
        $permission2 = Permission::create(['name'=>'role']);
        $permission3 = Permission::create(['name'=>'level']);
        $permission4 = Permission::create(['name'=>'station']);
        $permission5 = Permission::create(['name'=>'depaertment']);
        $permission6 = Permission::create(['name'=>'menu']);
        $permission7 = Permission::create(['name'=>'conlog']);
        $permission8 = Permission::create(['name'=>'loginlog']);
    }

    public function adduser(){
        $user = DB::insert('insert into users (id, name,email,password) values (?, ?,?,?)', [2, 'uio','789651@qq.com','123456']);
        $user = DB::insert('insert into users (id, name,email,password) values (?, ?,?,?)', [3, 'iop','789652@qq.com','123456']);
        return $user;
    }

    //赋予角色
    public function getrole(){
        $user1 = User::find(2);
        $user2 = User::find(3);
        $user1->assignRole('test1');
        $user2->assignRole('test2');
    }

    //权限赋予角色
    public function getpermission(){
        $role1 = Role::findById(2);
        $role2 = Role::findById(3);
        $role1->givePermissionTo(['member','role','level','station','depaertment','menu','conlog','loginlog']);
        $role2->givePermissionTo(['member','menu','conlog']);
    }

    //进入页面
    public function index(User $user){
        $menu = [];
        $allmenu = ["人员管理","角色管理","职级管理","岗位管理","部门管理","菜单管理","操作日志","登录日志"];
        $allflog = ['member','role','level','station','depaertment','menu','conlog','loginlog'];
        $backmenu=json_decode($user->getAllPermissions(),true);
        foreach ($backmenu as $key=>$value){
            for($i=0;$i<count($allflog);$i++){
                if($value["name"]==$allflog[$i]){
                    $menu[]=$allmenu[$i];
                }
            }
        }

        return view("home",["menu"=>$menu]);
    }
}
